package com.xjtu.platform.user.admin.extramanage.imply;


import com.xjtu.platform.user.admin.extramanage.dao.GetAdditionalApplyDao;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.user.admin.extramanage.GetAdditionalApplyDto;
import com.xjtu.platform.user.admin.extramanage.GetAdditionalApplyService;
import com.xjtu.platform.user.admin.extramanage.PermitExtraDto;
import com.xjtu.platform.user.admin.extramanage.RejectExtraDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Transactional
public class GetAdditionalApplyServiceImpl implements GetAdditionalApplyService {

    @Resource
    private GetAdditionalApplyDao getAdditionalApplyDao;


    @Override
    public Map<String, Object> getAdditionalApply(GetAdditionalApplyDto getAdditionalApplyDto) {
        return Utils.getPage(getAdditionalApplyDto, ()->getAdditionalApplyDao.getAdditionalApplyInfo(getAdditionalApplyDto));
    }

    @Override
    public boolean permitExtra(PermitExtraDto permitExtraDto) {
        try {
            String duty_id = getAdditionalApplyDao.getDuty_id();
            int id = Integer.parseInt(duty_id) + 1;
            duty_id = String.valueOf(id);
            String user_id = getAdditionalApplyDao.getUser_id(permitExtraDto);
            getAdditionalApplyDao.insertDuty(permitExtraDto,duty_id,user_id);
            int consult_ids = getAdditionalApplyDao.getConsult_id() + 1;
            String consult_id = String.valueOf(consult_ids);
            int count_num = getAdditionalApplyDao.getCount_num(permitExtraDto) + 1;
            getAdditionalApplyDao.insertConsult(permitExtraDto,consult_id,duty_id,count_num);
            getAdditionalApplyDao.updateAdditional_apply(permitExtraDto);
            String email = getAdditionalApplyDao.getConsultorEmail(permitExtraDto);
            Utils.sendMail(email,"恭喜追加咨询审核通过，请进入系统查询!!");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean rejectExtra(RejectExtraDto rejectExtraDto) {
        try {
            getAdditionalApplyDao.updateRejectState(rejectExtraDto);
            String email = getAdditionalApplyDao.getConsultorReEmail(rejectExtraDto);
            Utils.sendMail(email,"对不起，您的追加咨询审核未通过，请登陆系统查询详情");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
